package com.menggudf.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.menggudf.dao.PartsDao;
import com.menggudf.entity.Parts;
import com.menggudf.model.PartsModel;
import com.menggudf.model.ResultModel;
import com.menggudf.service.PartService;

@Service
public class PartServiceImpl implements PartService{
    
    private static final Logger LOGGER = LoggerFactory
            .getLogger(PartServiceImpl.class);
    
    @Autowired
    PartsDao partsDao;

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> queryAllPartGroups() {
        List<Parts> list = partsDao.getList();
        Map<String, List<PartsModel>> map = new HashMap<String, List<PartsModel>>();
        for(Parts parts : list){
            String onePart = parts.getOnePart();
            PartsModel model = new PartsModel();
            model.setId(parts.getId());
            model.setOnePart(onePart);
            model.setOneNum(parts.getOneNum());
            model.setTwoPart(parts.getTwoPart());
            model.setTwoNum(parts.getTwoNum());
            List<PartsModel> pl;
            if(map.get(onePart)==null){
                pl = new ArrayList<PartsModel>();
                pl.add(model);
                map.put(onePart, pl);
            }else{
                pl = map.get(onePart);
                pl.add(model);
            }
        }
        List<List<PartsModel>> lists = new ArrayList<List<PartsModel>>();
        for (Map.Entry<String, List<PartsModel>> entry : map.entrySet()) {
            lists.add(entry.getValue());
        }
        Map<String, Object> reMap = new HashMap<String, Object>();
        reMap.put("rows", lists);
        return reMap;
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel addPart(String onePart, String twoPart, Integer oneNum, Integer twoNum) {
        Parts part = partsDao.findByPart(onePart,twoPart);
        if(part!=null){
            return ResultModel.fail("部位已存在", null);
        }
        Parts parts = new Parts();
        parts.setOnePart(onePart);
        parts.setTwoPart(twoPart);
        parts.setOneNum(oneNum);
        parts.setTwoNum(twoNum);
        parts.setCreateTime(new Date());
        try {
            partsDao.persist(parts);
            return ResultModel.success("添加成功", null);
        } catch (Exception e) {
            LOGGER.error("addPart fail",e);
        }
        return ResultModel.fail("添加失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel eidtPart(Integer id, String onePart, String twoPart,Integer oneNum, Integer twoNum) {
        Parts parts = partsDao.findById(id);
        if(parts==null){
            return ResultModel.fail("疾病不存在", null);
        }
        if(StringUtils.hasText(onePart)){
            parts.setOnePart(onePart);
        }
        if(StringUtils.hasText(twoPart)){
            parts.setTwoPart(twoPart);
        }
        if(oneNum!=null){
            parts.setOneNum(oneNum);
        }
        if(twoNum!=null){
            parts.setTwoNum(twoNum);
        }
        try {
            partsDao.merge(parts);
            return ResultModel.success("修改成功", null);
        } catch (Exception e) {
            LOGGER.error("eidtPart fail",e);
        }
        return ResultModel.fail("修改失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel deletePart(Integer id) {
        Parts parts = partsDao.findById(id);
        if(parts==null){
            return ResultModel.fail("疾病不存在", null);
        }
        try {
            partsDao.remove(parts);
            return ResultModel.success("删除成功", null);
        } catch (Exception e) {
            LOGGER.error("deletePart fail",e);
        }
        return ResultModel.fail("删除失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> queryAllPartGroups(
            String onePart, String twoPart, int pageSize, int offset) {
        List<Parts> list = partsDao.getList(onePart,twoPart,pageSize,offset);
        long count = partsDao.getListCount(onePart,twoPart);
        List<PartsModel> mlist = new ArrayList<PartsModel>();
        for(Parts parts : list){
            PartsModel p = new PartsModel();
            p.setId(parts.getId());
            p.setOnePart(parts.getOnePart());
            p.setTwoPart(parts.getTwoPart());
            p.setOneNum(parts.getOneNum());
            p.setTwoNum(parts.getTwoNum());
            mlist.add(p);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", mlist);
        return map;
    }
}
